package cmpp.netty;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.demo.jfinal.model.ApiOrders;
import com.demo.jfinal.redis.running.ApiOrdersExecutor;
import com.google.gson.Gson;

import cmpp.pdu.Submit;

/**
 * 对不同请求进行处理
 * 待优化，放在队列里用多线程去处理
 * @author Guoxing
 *
 */
public class NettyCommandHandler {
    static protected Logger logger = Logger.getLogger(NettyCommandHandler.class.getName());
	/**
	 * 保存客户下发信息
	 * @param submit
	 */
	public static void saveSubmit2RedisQueue(Submit submit, String clientId) {
		String []climsg = clientId.split(",");
		String[] phoneNos = submit.getDestTermId();
		for (int i = 0; i < phoneNos.length; i++) {
			ApiOrders apiOrders = new ApiOrders();
			Date nowDate = new Date();
			String orderID = new SimpleDateFormat("yyyyMMddHHmmss").format(nowDate);
			String IDS1 = new SimpleDateFormat("ddSSS").format(nowDate);
			String IDS2 = new SimpleDateFormat("SSS").format(nowDate);
			apiOrders.set("ID", IDS1+IDS2);
			apiOrders.set("ORDER_ID", submit.getSrcId());
			apiOrders.set("CONF_ID", climsg[1]);
			apiOrders.set("CUSTOMER_NO", climsg[0]);
			String phone = phoneNos[i];
			if(phone.startsWith("86")){
				phone = phone.substring(2, 13);
			}
			apiOrders.set("PHONE_NO",phone );
			apiOrders.set("CONTENT", submit.getMsgContent());
			apiOrders.set("NUM", "1");
			apiOrders.set("DATE_TYPE", "0");
			try {
				ApiOrdersExecutor.setOrder(apiOrders);
				logger.info("saveSubmit 队列:"+new Gson().toJson(apiOrders)+" successful");
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("saveSubmit :"+new Gson().toJson(apiOrders)+" faile:"+e);
			}
		}
		
	}

}
